<template>
  <el-config-provider :locale="locale" :size="assemblySize" :button="buttonConfig">
    <router-view></router-view>
  </el-config-provider>
</template>

<script setup lang="ts">
import { onMounted, reactive, computed, ref } from "vue";
import { useI18n } from "vue-i18n";
import { getBrowserLang } from "@/utils";
import { useTheme } from "@/hooks/useTheme";
import { ElConfigProvider } from "element-plus";
import { LanguageType } from "./stores/interface";
import { useGlobalStore } from "@/stores/modules/global";
import en from "element-plus/es/locale/lang/en";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import { licenseList, cfg_softcheck } from "@/api/modules/system/params";
import { useUserStore } from "@/stores/modules/user";
import { cfgAppstate } from "@/api/modules/home";
import { addListener, launch } from "devtools-detector";
import { useRouter } from "vue-router";

const router = useRouter();
const globalStore = useGlobalStore();

// init theme
const { initTheme } = useTheme();
initTheme();

// init language
const i18n = useI18n();
onMounted(() => {
  const language = globalStore.language ?? getBrowserLang();
  i18n.locale.value = language;
  globalStore.setGlobalState("language", language as LanguageType);
  if (import.meta.env.MODE != "development") {
    // watchDebug();
  }
  if (useUserStore().token) {
    const isLogin = sessionStorage.getItem("isLogin");
    if (!isLogin) {
      localStorage.clear();
      sessionStorage.clear();
      window.location.reload();
    }
    useUserStore().setAppTitle();
  }
  // 监听标签页关闭和刷新
  window.addEventListener("beforeunload", e => beforeunloadHandler(e));
  window.addEventListener("unload", e => unloadHandler(e));
});
const beforeUnloadTime = ref(0);
const gapTime = ref(0);

function beforeunloadHandler() {
  beforeUnloadTime.value = new Date().getTime();
}

function unloadHandler() {
  gapTime.value = new Date().getTime() - beforeUnloadTime.value;
  //判断是窗口关闭还是刷新
  if (gapTime.value <= 5) {
    console.log("关闭窗口");
  } else {
    console.log("浏览器刷新");
  }
}

/**

 * @desc 监听控制台是否打开

 */

function watchDebug() {
  // 禁止右键

  document.addEventListener("contextmenu", e => {
    e.preventDefault();
  });

  // 禁止f12

  document.addEventListener("keydown", e => {
    if (e.code === "F12" || (e.ctrlKey && e.shiftKey && e.code === "KeyI") || (e.shiftKey && e.code === "F10")) {
      console.log("f12");

      e.preventDefault();
    }

    if (e.ctrlKey && e.keyCode == 85) {
      e.preventDefault();
    }
  });

  // 检测控制台是否被打开

  const timer = ref(null);

  function openDebugger() {
    if (timer.value) {
      timer.value = null;

      clearInterval(timer.value);
    }

    timer.value = setInterval(() => {
      debugger;
    }, 50);
  }

  function closeDebugger() {
    timer.value = null;

    clearInterval(timer.value);
  }

  addListener(isOpen => (isOpen ? openDebugger() : closeDebugger));

  launch();
}

// element language
const locale = computed(() => {
  if (globalStore.language == "zh") return zhCn;
  if (globalStore.language == "en") return en;
  return getBrowserLang() == "zh" ? zhCn : en;
});

// element assemblySize
const assemblySize = computed(() => globalStore.assemblySize);

// element button config
const buttonConfig = reactive({ autoInsertSpace: false });

// async function getCfg() {
//   if (!useUserStore().userInfo.name) return;
//   const res1 = await licenseList();
//   const res2 = await cfgAppstate();
//   const res3 = await cfg_softcheck();
//   console.log("res2=============",res2);
//   // useUserStore.setLicenseInfo({
//   //   isLicence: res1.result.data && res1.result.data.length,
//   //   license: res1.result.data[0][1] == 1 ? "测试版" : "正式版"
//   // });
//   localStorage.setItem(
//     "LicenseInfo",
//     JSON.stringify({
//       isLicence: Boolean(res1.result.data && res1.result.data.length),
//       license: res1.result.data[0][1],
//       version: res3.version,
//       licenseTime: res1.result.data[0][6],
//       day: res1.result.data[0][4],
//       running: res2.code > 0
//     })
//   );
//   // if (res1.result.data && res1.result.data.length) {
//   //   updateFormData.license = res1.result.data[0][1];
//   //   updateFormData.request = res1.result.data[0][6];
//   //   updateFormData.day = res1.result.data[0][4];
//   //   updateFormData.isLicence = true;
//   // } else {
//   //   updateFormData.isLicence = false;
//   // }
// }
</script>
